package com.mobinters.cafeman.database;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.mobinters.cafeman.object.Product;

public class DatabaseHandler extends SQLiteOpenHelper{

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "cafe_man";
	private static final String KEY_ID = "id";
	
	/*
	 * PRODUCT TABLE
	 */
	private static final String PRODUCT_TABLE = "product";
	private static final String PRODUCT_ID = "product_id";
	private static final String PRODUCT_CATEGORY = "product_category";
	private static final String PRODUCT_NAME = "product_name";
	private static final String PRODUCT_ICON_URL = "product_icon_url";
	private static final String PRODUCT_PRICE = "product_price";
	private static final String PRODUCT_DESCRIPTION = "product_description";
	private static final String PRODUCT_DETAIL_URL = "product_details";
	private static final String PRODUCT_TYPES = "product_types";
				

	public DatabaseHandler(Context context) {
		
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		//createProductTable();
	}

	public void createProductTable(){
		SQLiteDatabase db = this.getWritableDatabase();
		String CREATE_LOGIN_TABLE = "CREATE TABLE IF NOT EXISTS " + PRODUCT_TABLE + "("
			+ KEY_ID + " INTEGER PRIMARY KEY,"  //0
			+ PRODUCT_ID + " INTEGER,"			//1
			+ PRODUCT_CATEGORY + " INTEGER," 	//2
			+ PRODUCT_TYPES + " TEXT,"			//3
			+ PRODUCT_NAME + " TEXT,"			//4
			+ PRODUCT_ICON_URL + " TEXT,"		//5
			+ PRODUCT_PRICE + " DOUBLE,"		//6
			+ PRODUCT_DESCRIPTION + " TEXT,"	//7
			+ PRODUCT_DETAIL_URL + " TEXT"		//8
			+ ")";
			
		db.execSQL(CREATE_LOGIN_TABLE);
	}
	
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + PRODUCT_TABLE);
		onCreate(db);
	}
	
	public void addProduct(Product product){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		
		values.put(PRODUCT_ID,product.ProductId); 					//1
		values.put(PRODUCT_CATEGORY,product.ProductCategorieId); 	//2
		
		values.put(PRODUCT_TYPES,product.Types);					//3
		values.put(PRODUCT_NAME,product.ProductName1);				//4
		values.put(PRODUCT_ICON_URL,product.IconUrl);				//5
		values.put(PRODUCT_PRICE,product.ProductPrice);				//6
		values.put(PRODUCT_DESCRIPTION,product.Description);		//7
		values.put(PRODUCT_DETAIL_URL,product.DetailUrl);			//8
		
		String selectQuery = "SELECT  * FROM " + PRODUCT_TABLE + " WHERE "+ PRODUCT_ID + "='" + product.ProductId + "'";
				
		
		Cursor cursor = db.rawQuery(selectQuery, null);
		int rowCount = cursor.getCount();
		if(rowCount == 0){ 
			db.insert(PRODUCT_TABLE, null, values);
		}
		else 
		{
			cursor.moveToFirst();
			db.update(PRODUCT_TABLE, values, PRODUCT_ID +"='" + product.ProductId + "'", null);					
		}
	
		db.close();  
		 
	}
	
	public List<Product> getAllProduct(){
		
		SQLiteDatabase db = this.getReadableDatabase();
		String selectQuery = "SELECT  * FROM " + PRODUCT_TABLE;
		Cursor cursor = db.rawQuery(selectQuery, null);
		int rowCount = cursor.getCount();
		if(rowCount==0) return null;
		
		List<Product> pList = new ArrayList<Product>();
		Product product ;
		
		while(cursor.moveToNext()){
			product = new Product();
			product.ProductId = cursor.getInt(1);
			product.ProductCategorieId = cursor.getInt(2);
			product.Types = cursor.getString(3);
			product.ProductName1 = cursor.getString(4);
			product.IconUrl = cursor.getString(5);
			product.ProductPrice = cursor.getDouble(6);
			product.Description = cursor.getString(7);
			product.DetailUrl = cursor.getString(8);
			
			pList.add(product);
			
		}
		
		db.close();
		return pList;
				
	}
	
	public int getCount(){
		SQLiteDatabase db = this.getReadableDatabase();
		String selectQuery = "SELECT  * FROM " + PRODUCT_TABLE;
		Cursor cursor = db.rawQuery(selectQuery, null);
		int rowCount = cursor.getCount();
		db.close();
		return rowCount;
	}
	
	
	

}
